package books

import grails.test.*

class BookIntegrationTests extends GrailsUnitTestCase{

    void testDatabaseConnection(){
        def testBook = new Book(title: 'testTitle', author: 'testAuthor', 
            description: 'testDescription', ISBN: '0-943396-04-2', minimumGradeLevel: 2, maximumGradeLevel: 3,
            language: 'English', category: 'Health').save()
        
        def dbBook = Book.get(testBook.id)
        assertEquals(testBook.title, dbBook.title)
        
        testBook.title = 'newTitle'
        testBook.save()
        
        dbBook = Book.get(testBook.id)
        assertEquals (dbBook.title, testBook.title)
        
        dbBook.delete()
        assertFalse Book.exists(dbBook.id)
    }
    
    void testBookConstraints(){
        def testBook = new Book(title: 'testTitle', author: 'testAuthor', 
            description: 'testDescription', ISBN: '0-943396-04-2', minimumGradeLevel: 2, maximumGradeLevel: 3,
            language: 'English', category: 'Healt')
        
        assertNull testBook.save()
        
        testBook = new Book(title: 'testTitle', author: 'testAuthor', 
            description: 'testDescription', minimumGradeLevel: 4, maximumGradeLevel: 3,
            language: 'English', category: 'Health')
        
        assertNull testBook.save()
        
        testBook = new Book(title: 'testTitle', author: 'testAuthor', 
            description: 'testDescription', ISBN: 1234, minimumGradeLevel: 2, maximumGradeLevel: 3,
            language: 'English', category: 'Health')
        
        assertNull testBook.save()
    }
    
    def bookService
    void testBookService(){
        def testBook1 = new Book(title: 'testTitle1', author: 'testAuthor1', 
            description: 'testDescription', ISBN: '0-943396-04-2', minimumGradeLevel: 2, maximumGradeLevel: 3,
            language: 'English', category: 'Health').save()
        
        def testBook2 = new Book(title: 'testTitle2', author: 'testAuthor2', 
            description: 'testDescription', ISBN: '0-943396-04-2', minimumGradeLevel: 2, maximumGradeLevel: 3,
            language: 'English', category: 'Health').save()
        
        def testBook3 = new Book(title: 'testTitle3', author: 'testAuthor3', 
            description: 'testDescription', ISBN: '0-943396-04-2', minimumGradeLevel: 2, maximumGradeLevel: 3,
            language: 'English', category: 'Health').save()
        
        def randBook = bookService.getRandomBook()
        
        assertNotNull randBook
        
        def bookList = Book.list()
        assert bookList.contains(randBook)
    }
    
}
